package com.agentlab.imitation.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.agentlab.imitation.bean.TLqkAll;
import com.agentlab.imitation.bean.UserWish;
import com.agentlab.imitation.bean.Yx;
import com.agentlab.imitation.bean.Zy;
import com.agentlab.imitation.common.CommonConstant;
import com.agentlab.imitation.service.base.DaoSupport;

@Service
@Transactional
public class LqkServiceBean extends DaoSupport<TLqkAll> implements LqkService {
	@Resource
	SessionFactory factory;
	@Resource
	YxzydmService yxzydmService;

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void delete(Serializable... entityids) {
		// TODO Auto-generated method stub

	}

	/**
	 * 某院校某年的最低录取分
	 * 
	 * @param yxdm
	 * @param year
	 * @param mode
	 * @return
	 */
	private String getZdlqfByYxdm(String yxdm, String pcdm, String kldm, String year, int mode) {
		String zdlqf = "";
		String scoreModeHql = (mode == CommonConstant.MODE_BZF ? "bzzdlqf" : "zdlqf");
		String hql = "SELECT MIN(" + scoreModeHql + ")  FROM TLqkAll " + "  where pcdm=:pcdm and kldm=:kldm and yxdm=:yxdm and id.zsnf=" + year;
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("pcdm", pcdm).setParameter("kldm", kldm).setParameter("yxdm", yxdm).setCacheable(true);
		if (query.uniqueResult() != null)
			zdlqf = query.uniqueResult().toString();
		// ERROR:java.lang.Double cannot be cast to [Ljava.lang.Object
		// List list = query.list();
		// for (Iterator it = list.iterator(); it.hasNext();) {
		// Object[] d = (Object[]) it.next();
		// zdlqf = Double.parseDouble(d[0].toString()) ;
		// }
		return zdlqf;
	}
	/**
	 * 匹配某专业的所有院校
	 * @param bzzydm
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx>[] getYxlistByZyAll(String bzzydm, String pcdm, String kldm, String zyyxxh, String score, int mode){
		List[] yxListArray = new ArrayList[3];
		Map[] yxmaps = new HashMap[3];
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		for (int i = 0; i < years.length; i++) { //
			yxmaps[i] = getYxListByZyOneyear(years[i], bzzydm, pcdm, kldm, zyyxxh, score, i, mode, 300);
		}
		List yxdmList1 = new ArrayList();
		List yxdmList2 = new ArrayList();
		List yxdmList3 = new ArrayList();
		//三年符合标准
		List<Yx> yxlist3 = new ArrayList<Yx>();
		Iterator iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist3.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh) && yxmaps[2].containsKey(yxdh)) { // 在第二年和第三年中也找到此院校
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf2(yx2.getLqf2()); //2008
				yx1.setLqf1(yx3.getLqf1());  //2007
				yxlist3.add(yx1);
				yxdmList3.add(yx1.getYxdh());
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		//2年符合标准
		List<Yx> yxlist2 = new ArrayList<Yx>();
		iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist2.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh) && !yxdmList3.contains(yxdh)) { // 在第二年中也找到此院校,且没加入第三年列表
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
//				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yxlist2.add(yx1);
				yxdmList2.add(yx1.getYxdh());
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		//1年符合标准
		List<Yx> yxlist1 = new ArrayList<Yx>();
		iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist1.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if ( !yxdmList3.contains(yxdh) &&  !yxdmList2.contains(yxdh)) { 
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
//				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yxlist1.add(yx1);
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		
		//汇总三年
		yxListArray[0] = yxlist1; 
		yxListArray[1] = yxlist2;
		yxListArray[2] = yxlist3;
		
		return yxListArray;
	}

	/**
	 * 匹配某专业的院校列表(匹配三年）
	 * 
	 * @param bzzydm
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx> getYxlistByZyThreeYears(String bzzydm, String pcdm, String kldm, String zyyxxh, String score, int mode) {
		List<Yx> yxlist = new ArrayList<Yx>();
		Map[] yxmaps = new HashMap[3];
		// List lqklist = factory.getCurrentSession().createQuery(
		// " select distinct yxdm,yxdmmc from TLqkAll u where u.bzzydm=:bzzydm and u.pcdm=:pcdm and u.kldm = :kldm and u.id.zsnf in "
		// + CommonConstant.YEARS)
		// .setParameter("bzzydm", zydm).setParameter("pcdm",
		// pcdm).setParameter("kldm", kldm).list();
		// for (int i = 0; i < lqklist.size(); i++) {
		// TLqkAll lqk = (TLqkAll) lqklist.get(i);
		// yxlist.add(new Yx(Integer.parseInt( lqk.getYxdm()),lqk.getYxdmmc()));
		// // userid = users.getUserid();
		// }
		// for (Iterator it = lqklist.iterator(); it.hasNext();) {
		// Object[] d = (Object[]) it.next();
		// // System.out.println(d[0]);
		// // System.out.println(d[1]);
		// yxlist.add(new
		// Yx(Integer.parseInt(d[0].toString()),d[1].toString()));
		// }
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		for (int i = 0; i < years.length; i++) { //
			yxmaps[i] = getYxListByZyOneyear(years[i], bzzydm, pcdm, kldm, zyyxxh, score, i, mode, 300);
		}
		Iterator iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext()) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh) && yxmaps[2].containsKey(yxdh)) { // 在第二年和第三年中也找到此院校
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				yx1.setLqf2(yx2.getLqf2());
				yx1.setLqf1(yx3.getLqf1());
				yxlist.add(yx1);
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
				// yxlist.add((Yx) yxmaps[0].get(yxdh));// just for test
			}
		}
		return yxlist;
	}

	/**
	 * 匹配某专业的院校列表(匹配2年）
	 * 
	 * @param bzzydm
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx> getYxlistByZyTwoYears(String bzzydm, String pcdm, String kldm, String zyyxxh, String score, int mode) {
		List<Yx> yxlist = new ArrayList<Yx>();
		Map[] yxmaps = new HashMap[3];
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		for (int i = 0; i < years.length - 1; i++) { //
			yxmaps[i] = getYxListByZyOneyear(years[i], bzzydm, pcdm, kldm, zyyxxh, score, i, mode, 300);
		}
		Iterator iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh)) { // 在第二年中也找到此院校
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yxlist.add(yx1);
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
				// yxlist.add((Yx) yxmaps[0].get(yxdh));// just for test
			}
		}
		return yxlist;
	}

	/**
	 * 匹配某专业的院校列表(匹配1年）
	 * 
	 * @param bzzydm
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx> getYxlistByZyOneYear(String bzzydm, String pcdm, String kldm, String zyyxxh, String score, int mode) {
		List<Yx> yxlist = new ArrayList<Yx>();
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		Map<Integer, Yx> yxMap = getYxListByZyOneyear(years[0], bzzydm, pcdm, kldm, zyyxxh, score, 0, mode, 30);
		Iterator iterator = yxMap.keySet().iterator();
		while (iterator.hasNext() && yxlist.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			Yx yx1 = (Yx) yxMap.get(yxdh);
			// 填充各年最低录取分
			yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
			yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
			yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
			yxlist.add(yx1);
		}
		return yxlist;
	}

	/**
	 * 得到某一年，适合条件的院校列表
	 * 
	 * @param year
	 * @param zydm
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param index
	 *            年份序号
	 * @return
	 */

	private Map<Integer, Yx> getYxListByZyOneyear(String year, String bzzydm, String pcdm, String kldm, String zyyxxh, String score, int index,
			int mode, int maxResults) {
		Map<Integer, Yx> yxlist = new HashMap<Integer, Yx>();
		String scoreModeHql = (mode == CommonConstant.MODE_BZF ? "bzzdlqf" : "zdlqf");
		String hql = "SELECT yxdm,yxdmmc,zdlqf,bzzdlqf " + "  FROM TLqkAll "
				+ "  where bzzydm=:bzzydm and pcdm=:pcdm and kldm=:kldm and zyyxxh=:zyyxxh and id.zsnf=" + year
				+ "  group by yxdm,yxdmmc,zdlqf,bzzdlqf " + "  having MIN(" + scoreModeHql + ") <= :score" + "  order by " + scoreModeHql + " desc";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("bzzydm", bzzydm).setParameter("pcdm", pcdm).setParameter("kldm", kldm).setParameter("zyyxxh", Short.parseShort(zyyxxh))
				.setParameter("score", Double.parseDouble(score));
		query.setFirstResult(0).setMaxResults(maxResults);
		List list = query.list();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			Yx yx = new Yx(Integer.parseInt(d[0].toString()), d[1].toString());
			String lqf = d[2].toString();
			if (mode == CommonConstant.MODE_BZF)
				lqf = d[3].toString();
			if (index == 2) {
				yx.setLqf1(lqf);
			} else if (index == 1) {
				yx.setLqf2(lqf);
			} else if (index == 0) {
				yx.setLqf3(lqf);
			}
			yxlist.put(yx.getYxdh(), yx);
		}
		return yxlist;

	}

	/**
	 * 根据分数匹配院校（先选院校再选专业）
	 * 
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param index
	 *            数组下标，表示年份
	 * @param mode
	 * @param dymc
	 * @return
	 */
	private Map<Integer, Yx> getYxlist(String year, String pcdm, String kldm, String zyyxxh, String score, int index, int mode, String dymc,
			int maxResult) {
		System.out.println("LqkServiceBean.getYxlist() begin:dymc=" + dymc);
		Map<Integer, Yx> yxMap = new HashMap<Integer, Yx>();
		String scoreModeHql = (mode == CommonConstant.MODE_BZF ? "bzzdlqf" : "zdlqf");
		String hql = "SELECT yxdm,yxdmmc,zdlqf,bzzdlqf " + "  FROM TLqkAll " + "  where  pcdm=:pcdm and kldm=:kldm and zyyxxh=:zyyxxh and id.zsnf="
				+ year;
		if (!"".equals(dymc))
			hql += " and yxdm in (select distinct yxdm from TdYxdhYxdmAll where dymc in (" + dymc + "))"; // 地域限制
		hql += "  group by yxdm,yxdmmc,zdlqf,bzzdlqf " + "  having MIN(" + scoreModeHql + ") <= :score" + "  order by " + scoreModeHql + " desc";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("pcdm", pcdm).setParameter("kldm", kldm).setParameter("zyyxxh", Short.parseShort(zyyxxh)).setParameter("score",
				Double.parseDouble(score));
		query.setFirstResult(0).setMaxResults(maxResult);
		query.setCacheable(true);
		List list = query.list();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			Yx yx = new Yx(Integer.parseInt(d[0].toString()), d[1].toString());
			String lqf = d[2].toString(); // 原始分
			if (mode == CommonConstant.MODE_BZF)
				lqf = d[3].toString(); // 标准分
			if (index == 2) {
				yx.setLqf1(lqf);
			} else if (index == 1) {
				yx.setLqf2(lqf);
			} else if (index == 0) {
				yx.setLqf3(lqf);
			}

			yxMap.put(yx.getYxdh(), yx);
		}
		System.out.println("LqkServiceBean.getYxlist() end" );
		return yxMap;
	}

	/**
	 * 根据分数匹配院校（先选院校再选专业,一年符合标准）
	 * 
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx> getYxlistOneyear(String pcdm, String kldm, String zyyxxh, String score, int mode, String dymc) {
		List<Yx> yxlist = new ArrayList<Yx>();
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		Map<Integer, Yx> yxMap = this.getYxlist(years[0], pcdm, kldm, zyyxxh, score, 0, mode, dymc, 50);
		Iterator iterator = yxMap.keySet().iterator();
		while (iterator.hasNext() && yxlist.size() <= 30) { // 遍历
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			Yx yx = (Yx) yxMap.get(yxdh);
			// 填充各年最低录取分
			yx.setLqf1(this.getZdlqfByYxdm(yx.getYxdh().toString(), pcdm, kldm, years[2], mode));
			yx.setLqf2(this.getZdlqfByYxdm(yx.getYxdh().toString(), pcdm, kldm, years[1], mode));
			yx.setLqf3(this.getZdlqfByYxdm(yx.getYxdh().toString(), pcdm, kldm, years[0], mode));
			yxlist.add(yx);
		}
		return yxlist;
	}

	/**
	 * 根据分数匹配院校（先选院校再选专业,两年符合标准）
	 * 
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx> getYxlistTwoyears(String pcdm, String kldm, String zyyxxh, String score, int mode, String dymc) {
		List<Yx> yxlist = new ArrayList<Yx>();
		Map[] yxmaps = new HashMap[3];
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		for (int i = 0; i < years.length - 1; i++) { //
			yxmaps[i] = this.getYxlist(years[i], pcdm, kldm, zyyxxh, score, i, mode, dymc, 100);
		}
		Iterator iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh)) { // 在第二年中也找到此院校
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yxlist.add(yx1);
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		return yxlist;
	}

	/**
	 * 根据分数匹配院校（先选院校再选专业,三年符合标准）
	 * 
	 * @param pcdm
	 * @param kldm
	 * @param zyyxxh
	 * @param score
	 * @param mode
	 * @return
	 */
	public List<Yx> getYxlistThreeyears(String pcdm, String kldm, String zyyxxh, String score, int mode, String dymc) {
		List<Yx> yxlist = new ArrayList<Yx>();
		Map[] yxmaps = new HashMap[3];
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		for (int i = 0; i < years.length; i++) { //
			yxmaps[i] = this.getYxlist(years[i], pcdm, kldm, zyyxxh, score, i, mode, dymc, 100);
		}
		Iterator iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh) && yxmaps[2].containsKey(yxdh)) { // 在第二年和第三年中也找到此院校
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yxlist.add(yx1);
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		return yxlist;
	}
	
	public List<Yx>[] getYxlistAll(String pcdm,String kldm,String zyyxxh,String score,int mode,String dymc){
		List[] yxListArray = new ArrayList[3];
		Map[] yxmaps = new HashMap[3];
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		for (int i = 0; i < years.length; i++) { //
			yxmaps[i] = this.getYxlist(years[i], pcdm, kldm, zyyxxh, score, i, mode, dymc, 100);
		}
		List yxdmList1 = new ArrayList();
		List yxdmList2 = new ArrayList();
		List yxdmList3 = new ArrayList();
		//三年符合标准
		List<Yx> yxlist3 = new ArrayList<Yx>();
		Iterator iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist3.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh) && yxmaps[2].containsKey(yxdh)) { // 在第二年和第三年中也找到此院校
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
//				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
//				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
//				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yx1.setLqf2(yx2.getLqf2()); //2008
				yx1.setLqf1(yx3.getLqf1());  //2007
				yxlist3.add(yx1);
				yxdmList3.add(yx1.getYxdh());
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		//2年符合标准
		List<Yx> yxlist2 = new ArrayList<Yx>();
		iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist2.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if (yxmaps[1].containsKey(yxdh) && !yxdmList3.contains(yxdh)) { // 在第二年中也找到此院校,且没加入第三年列表
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
//				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));  //2009年已有
				yxlist2.add(yx1);
				yxdmList2.add(yx1.getYxdh());
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		//1年符合标准
		List<Yx> yxlist1 = new ArrayList<Yx>();
		iterator = yxmaps[0].keySet().iterator();
		while (iterator.hasNext() && yxlist1.size() <= 30) { // 遍历第三年（2009）
			String yxdhstr = iterator.next().toString();
			Integer yxdh = Integer.parseInt(yxdhstr);
			if ( !yxdmList3.contains(yxdh) &&  !yxdmList2.contains(yxdh)) { 
				Yx yx1 = (Yx) yxmaps[0].get(yxdh);
				Yx yx2 = (Yx) yxmaps[1].get(yxdh);
				Yx yx3 = (Yx) yxmaps[2].get(yxdh);
				// 填充各年最低录取分
				yx1.setLqf1(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[2], mode));
				yx1.setLqf2(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[1], mode));
//				yx1.setLqf3(this.getZdlqfByYxdm(yx1.getYxdh().toString(), pcdm, kldm, years[0], mode));
				yxlist1.add(yx1);
			} else { // 找不到此院校
				// yxmaps[0].remove(yxdh);
			}
		}
		
		//汇总三年
		yxListArray[0] = yxlist1; 
		yxListArray[1] = yxlist2;
		yxListArray[2] = yxlist3;
		
		return yxListArray;
	}

	/**
	 * 根据院校标准代码得到专业列表（以最后一年数据为准，即09年）
	 * 
	 * @param yxdm
	 * @param pcdm
	 * @param userWish
	 * @return
	 */
	public List<Zy> getZyListByYxdm(String yxdm, String pcdm, UserWish userWish) {
		List<Zy> zyList = new ArrayList<Zy>();
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		String year = years[0]; // "2009"
		String hql = "SELECT distinct bzzydm,bzzymc" + "  FROM TLqkAll " + "  where pcdm=:pcdm and kldm=:kldm and yxdm=:yxdm  and id.zsnf=" + year;
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("pcdm", pcdm);
		query.setParameter("kldm", userWish.getKl());
		query.setParameter("yxdm", yxdm);
		query.setFirstResult(0).setMaxResults(500);
		List list = query.list();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			Zy zy = yxzydmService.getYjxkdm(d[0].toString());
			// Zy zy = new Zy(d[0].toString(), d[1].toString());
			zyList.add(zy);
		}
		return zyList;
	}

	/**
	 * 根据院校标准代码和批次代码得到专业列表（以最后一年数据为准，即09年）
	 * 
	 * @param yxdm
	 * @param pcdm
	 * @return
	 */
	public List<Zy> getZyListByYxdmAndPcdm(String yxdm, String pcdm) {
		List<Zy> zyList = new ArrayList<Zy>();
		String[] years = CommonConstant.YEARS.substring(1, CommonConstant.YEARS.length() - 1).split(",");
		String year = years[0]; // "2009"
		String hql = "SELECT distinct bzzydm,bzzymc" + "  FROM TLqkAll " + "  where  yxdm=:yxdm and pcdm=:pcdm  and id.zsnf=" + year;
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("yxdm", yxdm);
		query.setParameter("pcdm", pcdm);
		query.setFirstResult(0).setMaxResults(500);
		List list = query.list();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object[] d = (Object[]) it.next();
			Zy zy = yxzydmService.getYjxkdm(d[0].toString());
			// Zy zy = new Zy(d[0].toString(), d[1].toString());
			zyList.add(zy);
		}
		return zyList;
	}

}
